import { useCallback, useEffect } from 'react';
import { useSystemStore } from './index';
import { selectors } from './selectors';

import type { UserInfo } from './slices/user';

// 主题相关hook
export const useTheme = () => {
  const theme = useSystemStore(selectors.theme);
  const setTheme = useSystemStore((state) => state.setTheme);
  const toggleTheme = useSystemStore((state) => state.toggleTheme);
  const isDarkMode = useSystemStore(selectors.isDarkMode);
  const activeTheme = useSystemStore(selectors.activeTheme);

  return {
    theme,
    setTheme,
    toggleTheme,
    isDarkMode,
    activeTheme,
  };
};

// 语言相关hook
export const useLanguage = () => {
  const language = useSystemStore(selectors.language);
  const setLanguage = useSystemStore((state) => state.setLanguage);

  return {
    language,
    setLanguage,
  };
};

// 加载状态hook
export const useLoading = () => {
  const isLoading = useSystemStore(selectors.isLoading);
  const setLoading = useSystemStore((state) => state.setLoading);

  return {
    isLoading,
    setLoading,
  };
};

// 错误状态hook
export const useError = () => {
  const error = useSystemStore(selectors.error);
  const hasError = useSystemStore(selectors.hasError);
  const setError = useSystemStore((state) => state.setError);
  const clearError = useSystemStore((state) => state.clearError);

  return {
    error,
    hasError,
    setError,
    clearError,
  };
};

// 用户认证hook
export const useAuth = () => {
  const userInfo = useSystemStore(selectors.userInfo);
  const isAuthenticated = useSystemStore(selectors.isAuthenticated);
  const isLoggingIn = useSystemStore(selectors.isLoggingIn);
  const login = useSystemStore((state) => state.login);
  const logout = useSystemStore((state) => state.logout);
  const setLoggingIn = useSystemStore((state) => state.setLoggingIn);

  const loginWithUser = useCallback(
    async (userInfo: UserInfo) => {
      setLoggingIn(true);
      try {
        await new Promise((resolve) => setTimeout(resolve, 1000));
        login(userInfo);
      } catch (error) {
        setLoggingIn(false);
        throw error;
      }
    },
    [login, setLoggingIn]
  );

  return {
    userInfo,
    isAuthenticated,
    isLoggingIn,
    login: loginWithUser,
    logout,
  };
};

// 用户信息hook
export const useUserInfo = () => {
  const userInfo = useSystemStore(selectors.userInfo);
  const displayName = useSystemStore(selectors.displayName);
  const updateUserInfo = useSystemStore((state) => state.updateUserInfo);
  const isAdmin = useSystemStore(selectors.isAdmin);
  const isUser = useSystemStore(selectors.isUser);
  const isGuest = useSystemStore(selectors.isGuest);

  return {
    userInfo,
    displayName,
    updateUserInfo,
    isAdmin,
    isUser,
    isGuest,
  };
};

// 设置相关hook
export const useAppSettings = () => {
  const settings = useSystemStore(selectors.settings);
  const updateSettings = useSystemStore((state) => state.updateSettings);
  const updateSetting = useSystemStore((state) => state.updateSetting);
  const resetSettings = useSystemStore((state) => state.resetSettings);

  return {
    settings,
    updateSettings,
    updateSetting,
    resetSettings,
  };
};

// 应用初始化hook
export const useAppInitialization = () => {
  const appInitialized = useSystemStore(selectors.appInitialized);
  const appConfig = useSystemStore(selectors.appConfig);
  const loadSettings = useSystemStore((state) => state.loadSettings);

  useEffect(() => {
    loadSettings();
  }, [loadSettings]);

  return {
    appInitialized,
    appConfig,
  };
};

// UI状态hook
export const useUIState = () => {
  const uiState = useSystemStore(selectors.uiState);
  const toggleSidebar = useSystemStore((state) => state.toggleSidebar);
  const toggleMobileMenu = useSystemStore((state) => state.toggleMobileMenu);

  return {
    ...uiState,
    toggleSidebar,
    toggleMobileMenu,
  };
};

// 菜单标签hook
export const useActiveTab = () => {
  const activeTab = useSystemStore(selectors.activeTab);
  const setActiveTab = useSystemStore((state) => state.setActiveTab);

  return {
    activeTab,
    setActiveTab,
  };
}; 